<?php
declare(strict_types=1);

namespace app\admin\middleware;

class Auth
{
    public function handle($request, \Closure $next)
    {
        // 前置中间件

        // 校验登录状态
        $noAuth       = !strstr($request->pathinfo(), "login") && !strstr($request->pathinfo(), "website") && !strstr($request->pathinfo(), "verify");
        $merchant     = session(config('merchant.session_merchant'));
        $loginSession = empty($merchant);
        if ($loginSession && $noAuth) {
            if ($request->isPost()) {
                return show(config("status.not_login"), '请重新登录');
            } else {
                $url = url("platform/login/index");
                echo exit('<script>top.location.href="' . $url . '"</script>');
            }
        }

        $websiteCode = $request->param('website_code', '', 'trim');
        if (!$websiteCode) {
            if ($request->isPost()) {
                return show(config("status.error"), '参数错误');
            } else {
                return redirect((string)url('platform/index/index'));
            }
        }

        return $next($request);
        // 后置中间件
    }

    public function end(\think\Response $response)
    {

    }
}
